var WxNotificationCenter = require('../../utils/WxNotificationCenter.js');
var comment = require("../../utils/utils.js")
var app = getApp();
var that;

Page({
	onLoad: function (options) {
        that = this;
        // 注册通知
        WxNotificationCenter.addNotification("poiSelectedNotification",that.getAddress,that);
        // 属于编辑状态
        if (options.objectId) {
            that.loadAddress(options.objectId);
            that.setData({
                isEdit: true
            });
            wx.setNavigationBarTitle({
                title: '编辑地址'
            })
        } else {
            wx.setNavigationBarTitle({
                title: '添加地址'
            })
        }
	},
	selectAddress: function () {
        console.log('tapped')
        // 跳转选择poi
		wx.navigateTo({
			url: '../search/search'
		});
	},
    getAddress: function (area) {
        // 选择poi地址回调
        that.setData({
            area: area
        });
    },
    add: function (e) {
        var form = e.detail.value;
        // console.log(form);
        // 表单验证
        if (form.realname == '') {
            wx.showModal({
                title: '请填写收件人姓名',
                showCancel: false
            });
            return;
        }

        if(!(/^1[34578]\d{9}$/.test(form.mobile))){ 
            wx.showModal({
                title: '请填写正确手机号码',
                showCancel: false
            });
            return;
        }

        if (form.detail == '') {
            wx.showModal({
                title: '请填写详细地址',
                showCancel: false
            });
            return;
        }
        form.gender = parseInt(form.gender);
        form.userId = app.globalData.openId;
        comment.PostJsonRequest("/wx/address/addressModify", form, function (res){
          if(res.data.data == 1){
            wx.showModal({
              title: '保存成功',
              showCancel: false,
              success: function () {
                wx.navigateBack();
              }
            });
          }else{
            wx.showModal({
              title: '保存失败',
              showCancel: false
            });
          }
        })
    },
    loadAddress: function (objectId) {
      //查询地址
      comment.GetRequest("/wx/address/queryAddressByAddressId",
        {
          "addressId": objectId
        }, function (addressObject){
          addressObject = addressObject.data;
          that.setData({
            address: addressObject
          });
      })
    },
    delete: function () {
        // 确认删除对话框
        wx.showModal({
            title: '确认删除',
            success: function (res) {
                if (res.confirm) {
                    var address = that.data.address;
                    comment.GetRequest("/wx/address/deleteAddressById",{
                        "addressId":address.addressId
                    },function(result){
                        wx.showModal({
                            title: '删除成功',
                            showCancel: false,
                            success: function () {
                                wx.navigateBack();
                            }
                        });
                    })
                }
            }
        });
        
    }
})